/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package kleinberg;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author veena
 */
public class Kleinberg {
String folder="";
List<Date> dates=new ArrayList<Date>();
List<String> ids=new ArrayList<String>();
List<Double> minute=new ArrayList<Double>();
String dfile="/home/veena/veena/f14/660/TG/obamacare/mitt2012/file1";
SimpleDateFormat dat=new SimpleDateFormat("EEE MMM dd HH:mm:ss +SSSS yyyy");;
        Calendar cal=Calendar.getInstance();

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Kleinberg kl=new Kleinberg();
        kl.mvc(args[0]);
    }
    private void mvc(String f){
        Kleinberg kl=new Kleinberg();
                SingleStream sst=new SingleStream();

        //parsing files
        //getting user tweets time and id
        kl.list(f);
                //extract minutes/now seconds and its date in order from prev step
                //i.e ascending order
//        kl.process();
//trial for lkienberg feed in op from prev step to single stream kleinberg
                //produces output for Kleinbergsinglestream
                //Format=A-B-C
//where A is the level,B is the start of this level and C is the end of this level
//        sst.preprocessing();
                //extract bursts for plotting
                //prints bursts for plotting
//               burstExtraction be=new burstExtraction();
//               be.readcosts();
        
        //get burts,reduce dimesionality
//        ScaleOutput so=new ScaleOutput();
//        so.scaleOuts();
        //*by 1000 and get actual dates for 
    }
    
    private void list(String f){
        Kleinberg kl=new Kleinberg();
        File folder = new File(f);///home/veena/Desktop/userTweets
File[] listOfFiles = folder.listFiles();

for (File file : listOfFiles) {
    if (file.isFile()) {
//        System.out.println(file.getName());
        kl.readfiles(file);
    }
    
}
    }
    private void readdates(){
        BufferedReader buff=null;
        Date d=null;
        ArrayList<String> tmp=new ArrayList<String>();
    try {
        
        buff = new BufferedReader(new FileReader((dfile)));
        String s=null,time="",te="";String[] spl=null;
        try {
            
            
            while((s=buff.readLine())!=null){
                if(!tmp.contains(s)){
//                    System.out.println(s);
                tmp.add(s);
                
                spl=s.split("-");
                time=spl[1].trim();
                
                try {
                    if(time.charAt(0)==':'){
                                            time=time.substring(1, time.length());
                                        }
                                        d=dat.parse(time);
                                        dates.add(d);
                                        ids.add(spl[0].trim());
                   
                } catch (ParseException ex) {
                    Logger.getLogger(Kleinberg.class.getName()).log(Level.SEVERE, null, ex);
                }
                }else{
//                System.err.println("Duplicate:"+s);
                }
            }
            tmp.clear();
        } catch (IOException ex) {
            Logger.getLogger(Kleinberg.class.getName()).log(Level.SEVERE, null, ex);
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Kleinberg.class.getName()).log(Level.SEVERE, null, ex);
    }finally{
            try {
                buff.close();
            } catch (IOException ex) {
                Logger.getLogger(Kleinberg.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    private void process(){
        //read ans get dates
        readdates();
        Collections.sort(dates);
            System.out.println(dates.size());
            //now convert milliseconds to seconds
            //get minutes and feed it t oklienberg...
            for(int i=0;i<dates.size();i++){
                cal.setTime(dates.get(i));
            long millis=cal.getTimeInMillis();
                    //millis to minutes
                    long minutes = TimeUnit.MILLISECONDS.toSeconds(millis);
                    minute.add((double)minutes);
                    System.out.println(minutes+"-"+dates.get(i)+"-"+ids.get(i));//
//                    System.out.println(minutes);
            }
    }
    private void readfiles(File f){
        BufferedReader buff=null;
        String[] spl=null,spl2=null,spl3=null;
        Date d=null;
        
        String id="",time="",hashtag="",tags;
        int stindex=0,endindex=0;
    try {
        String s=null;
        buff = new BufferedReader(new FileReader((f)));
        try {
            while((s=buff.readLine())!=null){
//                System.out.println(s);
                if(s.contains("created_at")){
                    spl=s.split("\\},\\{\"created_at\"");
                    
                    for(int i=0;i<spl.length;i++){
                        
                        //time
                        endindex=spl[i].indexOf(",\"id\":");
                        time=spl[i].trim().substring(0, endindex).replace("[{\"created_at\":\"", "").replace(",", "").replace("\"", "");
                        stindex=0;endindex=0;
                        //id
                        stindex=spl[i].indexOf(",\"id\":");
                        endindex=spl[i].indexOf(",\"id_str\":");
                        id=spl[i].substring(stindex+6, endindex);
                        stindex=0;endindex=0;
                        //hashtags
                        stindex=spl[i].indexOf("\"hashtags\":");
                        endindex=spl[i].indexOf(",\"urls\":");
                        if(stindex>0 & endindex>0 & endindex>stindex){
                                hashtag=spl[i].trim().substring(stindex+11, endindex).replace("[", "").replace("]", "").replace("", "");
                                if(hashtag.equals("") | hashtag.isEmpty()){
                                //empty no hashtag
                                }else{
        //                            System.out.println(spl[i].trim());
                                //look for text and search for particular hashtag
        //                            stindex=hashtag.indexOf("\"text\":\"");
        //                            endindex=hashtag.indexOf(",\"indices\"");
                                    spl2=hashtag.split(",\"indices\":");
                                    for(int j=0;j<spl2.length;j++){
                                        if(spl2[j].contains("text")){
                                            stindex=spl2[j].indexOf("\"text\":");
                                            tags=spl2[j].trim().substring(stindex+7).replace("\"", "");
                                            if(tags.toLowerCase().equals("mitt2012")){
                                                id=id+"-"+time;
                                                if(!ids.contains(id)){
                                                    ids.add(id);
                                                    System.out.println(id+"-"+tags);
                                                }
                                            }
                                        }
                                    }
                                    spl2=null;

//                                    try {
//                                        if(time.charAt(0)==':'){
//                                            time=time.substring(1, time.length());
//                                        }
//                                        d=dat.parse(time);
//                                    } catch (ParseException ex) {
//                                        Logger.getLogger(Kleinberg.class.getName()).log(Level.SEVERE, null, ex);
//                                    }
//                                    cal.setTime(d);
//                                    dates.add(d);
//                                    ids.add(id);
                                }
                            }
                        stindex=0;endindex=0;
//                        id=spl2[0].trim();
                        spl2=null;
                    }spl=null;
//                    time=spl[0].trim().replace("[{\"created_at\":\"", "").replace(",", "").replace("\"", "");
//                    id=spl[1].trim().replace("\"id\":", "").replace(",", "").replace("\"str\"", "");
                    
                    
//System.out.println(millis);
             
//                     Collections.sort(dates, c);
//                            for(int i=0;i<spl.length;i++){
                    //sort time and add it to hashmap
//                    }
                }
            }
            buff.close();
        } catch (IOException ex) {
            Logger.getLogger(Kleinberg.class.getName()).log(Level.SEVERE, null, ex);
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Kleinberg.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            buff.close();
        } catch (IOException ex) {
            Logger.getLogger(Kleinberg.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    }
    
}
